/*
 *    Copyright © OpenAtom Foundation.
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */

package com.inspur.edp.bef.dtgenerator.api.entityInterface;

import com.inspur.edp.bef.bizentity.operation.BizAction;
import com.inspur.edp.bef.component.detailcmpentity.be.BEComponent;
import com.inspur.edp.bef.component.detailcmpentity.be.BEMethod;
import com.inspur.edp.bef.component.detailcmpentity.be.BEMethodParameter;
import com.inspur.edp.bef.dtgenerator.api.BefApiGeneratorContext;
import com.inspur.edp.bef.dtgenerator.common.Utils;
import com.inspur.edp.caf.generator.baseInfo.InterfaceMethodGenerateInfo;
import com.inspur.edp.caf.generator.baseInfo.ParameterInfo;
import com.inspur.edp.caf.generator.baseInfo.TypeInfo;
import com.inspur.edp.caf.generator.method.InterfaceMethodGenerator;
import com.inspur.edp.caf.generator.method.InterfaceMethodGeneratorContext;
import com.inspur.edp.lcm.metadata.api.entity.GspMetadata;
import org.eclipse.jdt.core.dom.Modifier.ModifierKeyword;

public class BEInterfaceMethodGenerator extends InterfaceMethodGenerator {
  private BefApiGeneratorContext apiGenCtx;
  private BizAction action;
  private BEComponent beComp;
  private BEMethod beMethod;

  public BEInterfaceMethodGenerator(BefApiGeneratorContext apiGenCtx, BizAction action) {
    this.apiGenCtx = apiGenCtx;
    this.action = action;
  }

  @Override
  protected InterfaceMethodGeneratorContext createClassInfoContext() {
    return new InterfaceMethodGeneratorContext();
  }

  @Override
  protected InterfaceMethodGenerateInfo createClassMethodInfo() {
    return new InterfaceMethodGenerateInfo();
  }

  @Override
  protected void doInitialize() {
    GspMetadata metadata =
        apiGenCtx
            .getBeGenCtx()
            .getMetadataPool()
            .getMetadata(action.getComponentPkgName(), action.getComponentId());
    beComp = (BEComponent) metadata.getContent();
    if (beComp == null) {
      throw new RuntimeException(
          /*ErrorCode.BeCompNotFound*/
          Utils.formatExceptionMsg("BE动作构件不存在: " + action.getComponentId()));
    }
    beMethod = beComp.getBeMethod();
  }

  @Override
  protected java.util.ArrayList<ModifierKeyword> getAccessModifiers() {
    return null;
  }

  @Override
  protected String getMethodName() {
    return action.getCode();
  }

  @Override
  protected java.util.ArrayList<ParameterInfo> getParameterCollection() {
    java.util.ArrayList<ParameterInfo> result = new java.util.ArrayList<ParameterInfo>();
    for (Object par : beMethod.getBeParams()) {
      result.add(com.inspur.edp.cef.generator.overall.Utils.buildParGenInfo((BEMethodParameter) par, apiGenCtx, null));
    }
    return result;
  }

  @Override
  protected TypeInfo getReturnType() {
    try{
      return com.inspur.edp.cef.generator.overall.Utils.getReturnType(beMethod, apiGenCtx.getBeGenCtx());
    }catch (Exception e){
      throw new RuntimeException("方法"+action.getCode()+"返回值解析异常", e);
    }

  }
}
